﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MusicMashup.Models
{
    public class MusicRepository
    {
        private MusicDBDataContext db;
        
        public MusicRepository()
        {
            db = new MusicDBDataContext();
        }

        #region CRUDS's for Song Table 

        public IQueryable<Song> GetSongs()
        {
            return db.Songs.Select(x => x);
        }

        public Song GetSongById(int id)
        {
            return db.Songs.Where(x => x.Id == id).SingleOrDefault();
        }

        public IQueryable<Song> GetSongsByKeywords(String keywords)
        {
            return from song in db.Songs
                   where song.Name.Contains(keywords)
                   || song.Artist.Contains(keywords)
                   || song.Genre.Contains(keywords)
                   select song;
        }

        public void AddSong(Song song)
        {
            db.Songs.InsertOnSubmit(song);
            db.SubmitChanges();
        }

        public void UpdateSong(int id, String title, String artist, String genre)
        {
            Song song = GetSongById(id);
            if (song != null)
            {
                song.Name = title;
                song.Artist = artist;
                song.Genre = genre;
                db.SubmitChanges();
            }
        }

        #endregion

    }
}